package com.my.blog.website.controller.user;


import com.my.blog.website.constant.WebConst;
import com.my.blog.website.controller.BaseController;
import com.my.blog.website.modal.Vo.CommentVo;
import com.my.blog.website.service.ICommentService;
import com.my.blog.website.service.IContentService;
import com.my.blog.website.utils.TaleUtils;
import com.vdurmont.emoji.EmojiParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by geng on 2019/10/22.
 */
@Controller
public class CommentShowController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private ICommentService commentService;

    @Autowired
    private IContentService blogService;

    @Value("${comment.avatar}")
    private String avatar;

    @GetMapping("/comments/{blogId}")
    public String comments(@PathVariable Integer blogId, Model model) {
        model.addAttribute("comments", commentService.getComments(blogId, 1, WebConst.MAX_POSTS));
        return "blog :: commentList";
    }


    @PostMapping("/comments")
    public String post(CommentVo comment, HttpServletRequest request, HttpSession session) {
        LOGGER.info(comment.getCoid() + "**" + comment.getCid() + "**" + comment.getAuthor() + "**" + comment.getMail() + "**" + comment.getContent());
        Integer blogId = comment.getCid();
        comment.setCid(blogId);
        String author = TaleUtils.cleanXSS(comment.getAuthor());
        String text = TaleUtils.cleanXSS(comment.getContent());

        author = EmojiParser.parseToAliases(author);
        text = EmojiParser.parseToAliases(text);

        CommentVo comments = new CommentVo();
        comments.setAuthor(author);
        comments.setCid(comment.getCid());
        comments.setIp(request.getRemoteAddr());
        comments.setUrl(comment.getUrl());
        comments.setContent(text);
        comments.setMail(comment.getMail());
        comments.setParent(comment.getCoid());
        commentService.insertComment(comments);
        return "redirect:/comments/" + blogId;
    }


}
